package com.example.game_app;

import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.game_app.adapter.MenuAdapter;
import com.example.game_app.fragment.GameListViewFragment;
import com.example.game_app.fragment.GameMatchViewFragment;
import com.example.game_app.fragment.GameStarViewFragment;
import com.example.game_app.fragment.GameStrategyViewFragment;
import com.example.game_app.util.UserContext;

import java.util.ArrayList;

public class ViewPagerActivity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager viewPager;//  轮播图
    private ArrayList<Fragment> fragmentList;
    private TextView game_recommend_button, game_match_button, game_star_button, game_strategy_button;
    // 获取从 游戏页面传递的值
    private int StrategyPosition;
    private String GameName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);// 主页


        // 菜单  点击事件
        game_recommend_button = findViewById(R.id.game_recommend_button);
        game_match_button = findViewById(R.id.game_match_button);
        game_star_button = findViewById(R.id.game_star_button);
        game_strategy_button = findViewById(R.id.game_strategy_button);


        // 事件触发
        game_recommend_button.setOnClickListener(this);
        game_match_button.setOnClickListener(this);
        game_star_button.setOnClickListener(this);
        game_strategy_button.setOnClickListener(this);


        viewPager = findViewById(R.id.viewPager);// 轮播图

        fragmentList = new ArrayList<>();
        fragmentList.add(new GameListViewFragment());
        fragmentList.add(new GameMatchViewFragment());
        fragmentList.add(new GameStarViewFragment());
        fragmentList.add(new GameStrategyViewFragment());

        // 跳转攻略页面，gameName查询
        StrategyPosition = getIntent().getIntExtra("StrategyPosition", 0);
        GameName = getIntent().getStringExtra("GameName");
        viewPage();// 轮播图初始加载
    }

    public void viewPage() {
        // 使用FragmentPagerAdapter
        PagerAdapter mPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };

        viewPager.setAdapter(mPagerAdapter);
        // 如果有传值（gameName），则跳转到游戏攻略页面
        if (StrategyPosition != 0) {
            viewPager.setCurrentItem(StrategyPosition);
            GameStrategyViewFragment fragment = (GameStrategyViewFragment) fragmentList.get(3);
            Bundle bundle = new Bundle();
            bundle.putString("GameName", GameName);
            fragment.setArguments(bundle);
            setColor4();
        } else {
            viewPager.setCurrentItem(0); // 页面设置
        }
    }

    @Override
    public void onClick(View view) {
        // ID匹配 按钮ID
        if (view.getId() == R.id.game_recommend_button) {
            viewPager.setCurrentItem(0);
            setColor1();
        } else if (view.getId() == R.id.game_match_button) {
            viewPager.setCurrentItem(1);
            //日期选择的重置，刷新比赛列表
            GameMatchViewFragment fragment = (GameMatchViewFragment) fragmentList.get(1);
            fragment.getMatchList(view);
            if(fragment.menuAdapter != null) {
                fragment.menuAdapter.resetSelectPosition();
            }else {
                Log.e("menuAdapter未初始化","fragment.menuAdapter is null");
            }
            // 设置背景颜色
            setColor2();
        } else if (view.getId() == R.id.game_star_button) {
            viewPager.setCurrentItem(2);
            // 设置背景颜色
            setColor3();
        } else if (view.getId() == R.id.game_strategy_button) {
            viewPager.setCurrentItem(3);
            // 设置背景颜色
            setColor4();
        }

    }

    public void setColor1() {
        // 设置背景颜色
        game_recommend_button.setBackgroundColor(getResources().getColor(R.color.activeBackground));
        game_match_button.setBackgroundColor(getResources().getColor(R.color.unactiveBackground));
        game_star_button.setBackgroundColor(getResources().getColor(R.color.unactiveBackground));
        game_strategy_button.setBackgroundColor(getResources().getColor(R.color.unactiveBackground));
        // 设置字体加粗
        game_recommend_button.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        game_match_button.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        game_star_button.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        game_strategy_button.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
    }

    public void setColor2() {
        // 设置背景颜色
        game_recommend_button.setBackgroundColor(getResources().getColor(R.color.unactiveBackground));
        game_match_button.setBackgroundColor(getResources().getColor(R.color.activeBackground));
        game_star_button.setBackgroundColor(getResources().getColor(R.color.unactiveBackground));
        game_strategy_button.setBackgroundColor(getResources().getColor(R.color.unactiveBackground));
        // 设置字体加粗
        game_recommend_button.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        game_match_button.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        game_star_button.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        game_strategy_button.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
    }

    public void setColor3() {
        // 设置背景颜色
        game_recommend_button.setBackgroundColor(getResources().getColor(R.color.unactiveBackground));
        game_match_button.setBackgroundColor(getResources().getColor(R.color.unactiveBackground));
        game_star_button.setBackgroundColor(getResources().getColor(R.color.activeBackground));
        game_strategy_button.setBackgroundColor(getResources().getColor(R.color.unactiveBackground));
        // 设置字体加粗
        game_recommend_button.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        game_match_button.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        game_star_button.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        game_strategy_button.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
    }

    public void setColor4() {
        // 设置背景颜色
        game_recommend_button.setBackgroundColor(getResources().getColor(R.color.unactiveBackground));
        game_match_button.setBackgroundColor(getResources().getColor(R.color.unactiveBackground));
        game_star_button.setBackgroundColor(getResources().getColor(R.color.unactiveBackground));
        game_strategy_button.setBackgroundColor(getResources().getColor(R.color.activeBackground));
        // 设置字体加粗
        game_recommend_button.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        game_match_button.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        game_star_button.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        game_strategy_button.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    }


}